package com.ll.genericHomework;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * @author 刘龙
 * @version 1.0
 */
public class TestDemo {
    public static void main(String[] args) {
        ArrayList<Emp> emps = new ArrayList<>();
        emps.add(new Emp("cc", 99));
        emps.add(new Emp("aa", 20));
        emps.add(new Emp("cc", 60));
        System.out.println(emps);
        emps.sort(new Comparator<Emp>() {
            @Override
            public int compare(Emp emp1, Emp emp2) {
                if (!(emp1 instanceof Emp && emp2 instanceof Emp)) {
                    return 0;
                }

                int i = emp1.getName().compareTo(emp2.getName());
                if (i != 0) {
                    return i;
                }

                return emp1.getAge() - emp2.getAge();
            }
        });
        System.out.println(emps);

        Dao2<Emp> empDao = new Dao2<>();
        empDao.save("001", new Emp("ll1", 11));
        empDao.save("002", new Emp("ll2", 22));
        empDao.save("003", new Emp("ll3", 33));

        System.out.println(empDao.getlist());

        System.out.println(empDao.get("003"));
        empDao.remove("003");
        System.out.println(empDao.getlist());
        empDao.update("001", new Emp("ll1", 1111111));
        System.out.println(empDao.getlist());


    }
}

class Emp {
    private String name;
    private Integer age;

    public Emp(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class Dao2<T> {
    private Map<String, T> map = new HashMap<>();

    public void save(String string, T t) {
        map.put(string, t);
    }

    public void update(String string, T t) {
        map.put(string, t);
    }

    public void remove(String string) {
        map.remove(string);
    }

    public List<T> getlist() {
        ArrayList<T> arrayList = new ArrayList<>();
        Set<String> strings = map.keySet();
        for (String string : strings) {
            arrayList.add(get(string));
        }
        return arrayList;
    }

    public T get(String string) {
        return map.get(string);
    }

}

class AAAAA {
    @Test
    public void test() {
        Fish<String, String> stringIntegerFish = new Fish<>();
        stringIntegerFish.hello(1.0d, 1.0, "ll", 123);
    }
}


class Fish<T, R> {

    /**
     * 泛型方法, 可以使用类声明的泛型,也可以使用自己声明的泛型
     */
    public <A, B, T> void hello(A a, B b, R r, T t) {
        System.out.println(t.getClass());
    }
}
